package com.hmy.finance.service.receipt.dto;

import cn.hutool.core.bean.BeanUtil;
import com.hmy.ccp.common.dto.ContextUserInfo;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.ccp.starter.web.utils.UserContextUtil;
import com.hmy.finance.api.bo.core.request.enums.ReceiptOffsetBillStatusEnum;
import com.hmy.finance.api.bo.core.request.receiptOffset.ReceiptOffsetRevokeRequest;
import com.hmy.finance.dal.po.receipt.ReceiptOffsetBillPo;
import com.hmy.workflow.api.bo.request.process.RevokeProcessRequest;
import java.io.Serializable;
import java.util.Map;
import lombok.Data;

@Data
public class ReceiptOffsetRevokeDto implements Serializable {

    /**
     * 核销单id
     */
    private Long receiptOffsetBillId;

    /**
     * 撤销备注
     */
    private String comment;

    /**
     * 当前审批节点,用于撤回
     * AREA_MANAGER:表示销售经理撤回
     * CASHIER: 表示区域经理撤回
     * ARACCOUNTANT: 表示出纳会计撤回
     */
    private String approvalNode;

    /**
     * 当前审批节点枚举
     */
    private com.hmy.finance.api.enums.ReceiptOffsetApprovalNodeEnum approvalNodeEnum;

    /**
     * 核销单
     */
    private ReceiptOffsetBillPo receiptOffsetBillPo;
    
    public static ReceiptOffsetRevokeDto convertToDto(ReceiptOffsetRevokeRequest request){
        ReceiptOffsetRevokeDto receiptOffsetRevokeDto = BeanUtil.copyProperties(request, ReceiptOffsetRevokeDto.class);
        receiptOffsetRevokeDto.setApprovalNodeEnum(com.hmy.finance.api.enums.ReceiptOffsetApprovalNodeEnum.getEnumByValue(receiptOffsetRevokeDto.getApprovalNode()));
        return receiptOffsetRevokeDto;
    }

    public RevokeProcessRequest   buildRevokeProcessRequest(ReceiptOffsetBillStatusEnum toRevokeBillStatusEnum) {
        RevokeProcessRequest revokeProcessRequest = new RevokeProcessRequest();
        revokeProcessRequest.setProcessInstanceId(receiptOffsetBillPo.getProcessInstanceId());
        revokeProcessRequest.setComment(this.getComment());
        ReceiptOffsetBillProcessVariableDto variableDto = BeanUtil.copyProperties(receiptOffsetBillPo, ReceiptOffsetBillProcessVariableDto.class);
        ContextUserInfo userInfo = UserContextUtil.getCurrentUser();
        variableDto.setUpdaterId(userInfo.getId());
        variableDto.setUpdaterName(userInfo.getUserName());
        variableDto.buildReceiptOffsetBillStatus(toRevokeBillStatusEnum);
        Map<String, Object> variables = BeanUtil.beanToMap(variableDto);

        //根据当前节点撤销上一个节点,并且审批节点变成上一个节点
        switch (approvalNodeEnum) {
            case AREA_MANAGER:
                //下一个审批节点
                variables.put("approvalNode", null);
                variables.put("rejectCount", 1);
                break;
            case CASHIER:
                //下一个审批节点
                variables.put("approvalNode", com.hmy.finance.api.enums.ReceiptOffsetApprovalNodeEnum.AREA_MANAGER.getCode());
                variables.put("rejectCount", 1);
                break;
            case ARACCOUNTANT:
                variables.put("approvalNode", com.hmy.finance.api.enums.ReceiptOffsetApprovalNodeEnum.CASHIER.getCode());
                variables.put("rejectCount", 1);
                break;
            default:
                throw new HMYRuntimeException(506900009);
        }
        revokeProcessRequest.setVariables(variables);
        return revokeProcessRequest;
    }
}
